package com.dayfly.tip.action;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.struts2.interceptor.validation.SkipValidation;

import com.dayfly.tip.ao.RecordAO;
import com.dayfly.tip.model.CommonResult;
import com.dayfly.tip.model.RecordDO;
import com.dayfly.tip.model.Result;
import com.opensymphony.xwork2.ActionContext;

/**
 * 
 * @author dayfly
 * 
 */
public class RecordAction extends BaseAction {

	private static final long serialVersionUID = 4037172436770142636L;
	
	public static final String RETURNSHOW = "show";

	public static final String UPDATETYPE = "update";

	private RecordAO recordAO;

	public void setRecordAO(RecordAO recordAO) {
		this.recordAO = recordAO;
	}

	private String body;

	private String subject;

	private String jsonResult;

	private long id;

	private String type;

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}

	public String getBody() {
		return body;
	}

	public String getSubject() {
		return subject;
	}

	public String getTags() {
		return tags;
	}

	private String tags;

	public void setBody(String body) {
		this.body = body;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public void setTags(String tags) {
		this.tags = tags;
	}

	@Override
	public String execute() throws Exception {
		ActionContext tx = ActionContext.getContext();
		Long userId = (Long) tx.getSession().get("userId");
		String userNick = (String) tx.getSession().get("userNick");

		if (userId == null) {
			return LOGIN;
		}

		RecordDO record = new RecordDO();
		record.setBody(this.body);
		record.setSubject(this.subject);
		record.setTags(this.tags);
		record.setUserId(userId);
		record.setUserNick(userNick);
		
		Result result = new CommonResult(false);

		if (this.getType() !=null && this.getType().equals(UPDATETYPE)) {
			if (getId() <= 0) {
				return ERROR;
			}
			record.setId(getId());
			result = recordAO.updateRecord(record);
			result.setSuccess(true);
			tx.getValueStack().set("itemId",record.getId());
			return RETURNSHOW;
		} else {
			result = recordAO.record(record);
			result.setSuccess(true);
		}
		if (result.isSuccess()) {
			tx.getValueStack().set("result", result);
			tx.getValueStack().set("hello", "hello");
			tx.getValueStack().set("result", result);
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

	public String modify() {
		if (getId() != 0) {
			Result result = recordAO.deleteRecordById(getId());
			Map<String, Object> para = new HashMap<String, Object>();
			para.put("success", result.isSuccess());
			JSONObject o = JSONObject.fromObject(para);
			setJsonResult(o.toString());
		}
		return "result";
	}

	@SkipValidation
	public String delete() {
		if (getId() != 0) {
			Result result = recordAO.deleteRecordById(getId());
			Map<String, Object> para = new HashMap<String, Object>();
			para.put("success", result.isSuccess());
			JSONObject o = JSONObject.fromObject(para);
			setJsonResult(o.toString());
		}
		return "result";
	}

}
